package utils;
import com.alibaba.fastjson.JSON;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import dto.UserInfoDTO;

import java.util.Base64;

/**
 * @program: sporadic
 * @description:
 * @author: LHD   QQ:1727865856
 * @create: 2019-11-16 13:46
 **/
public class JwtSecret {
   public static final String SECRET="MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCNL+tOo/D7fMtVUjnEGpK4gt5iLpb5PIyWtnsXcYTczfZli0Zm3Nav3a66+JP3AneEe2M/Em2x5SxZiwFN1/fPpWK+JHaVHsGmDzJ9bW1+1dTM7Kxj9825jy29ythtLB/0S8vU/mfJ3pcUlUniytWNW1HDWb4/otUDHY6ta5I/4mM5GJDLBffUZjL+tpWVppMwmwGbKoBzh3WCRo5IjxXQAN1hsP7LRXvnUGvUhC5sYJh0YWI3DxJHONwW/GQXpx18Vmf6sFmBCZePNKdMKaV4bDlgwYlFUV6ns+b/OhQjvUcS+/422+rfJ9Dyj6gzzGiiSvlvhjTTpQQtGDqvU85hAgMBAAECggEAVjI6O6G1PyR6UxXHNKVwi/tB37lH7NP9lQnFbpOcpN2Nn4L/MU+ENoxl37phQ32bECPwfJI4rFIY2HYYDrlajsgO170VNIc5fLNshMzo+3V63L1lpgak77OtHmEX7tbwj0ozIE9NeB5QKH3YRQDGje2Bpn1EvD8iQyupbtDs0djwP1juBVgMs+8tHcw7BiLnqVQnV+miKkSPfIRGOhGkRu2n7KBAuEkEpg3ClrjBWjmqPeM8feoZnC3nV7EHQBuB416PweCFwtXng2XtVXihZpfbb1CwWDbsUEpOosHWY6WKNz1y1p44aZT5u/bRQst/A/mGAlF1kwQSIcMYrBuw3QKBgQDVbr9duyyVf6ORbLcwFOzZJtCVSDk6VUOnwOwY2mdI0DEe8D5MTskwjxbsHTOxLa+AuPjPo2AQzaSO3g2bHvPskSXz+tKYTzqX7PLJcRwx4fFVfUFL8LiWn5xgxMcny17VLnuTaHkslm3OKjZbjGP0BNUb+tvpIKL5evA1e8PLcwKBgQCpWIdspPJoRnEZGJN1le4CDisVXXYZ0ubYY3/Eu/plit8MqOtuBy6OXaDlkQtZcMIE+0+hOldYtO0UjkbJbMWhfx0cRTiraaRLYxWrVsnRCF34T426r3aAq7DguYCM8BwsPIFTREKXiaC8XSy9msRD3SbFJlqp9ICswGqi0h1x2wKBgQCv60KsfVS4PKaTfSpY5UZ7jjSqdwdEf3buVGPF8G7lY0S5nYeb2eyWq7pXD5LF3UQlM1FRw/Wwo++ZGonYa+DeAp1nxjvC8rr28oUDaMm2i+chccZaU6Wx8Qkx8b+9mzElQdT1oU0PZUZIgh6fws24GiuVjmWoeBAc1ioby1GM5QKBgQConqX54CQfAEUNIBN0Od3L3h+4Hl/CRuXlAO3wDty8zxJZoLkLzcrlpwTWkR4j6MrH+xTHDTh782s91I1Xcx82N0PdKm/K/l3BUt/4w2og4Zvv7AbLpHrylAKDQTJnI45p3bVIG+fSzA9of75U7Q4QJNy94frUz8hwOwfzKBR5kQKBgQDLJbLVJCxpwXYtiHUJQzot5mKOulzwgceVXb3lC0xvfzXc6Nxe/6F3dRkalJrX4ZUjVv0BRKzlZO2gBSJCUpbn/Dfj3GCzmkKRXDuZm+6GWjsqlJpoH4cKkAnl/Z5pUJ/yh4GWrfv4eK4OP+8Ld74m8yHjL9kuf40Bz9Mc1rsoXA==";

    /**
     * 验证token是否是有效的
     * @param token
     * @return
     */
    public static String vladateToken(String token){
        Algorithm algorithm = Algorithm.HMAC256(JwtSecret.SECRET);
        JWTVerifier jwtVerifier= JWT.require(algorithm).build();
        //验签
        DecodedJWT decodedJWT = jwtVerifier.verify(token);
        //获取用户信息
        String payLoad = decodedJWT.getPayload();
        byte[] bs = Base64.getDecoder().decode(payLoad);
        String json = new String(bs);
        System.out.println(json);
        System.out.println("验证成功");
        if (json!=null||json!="") return json;
        else return "";
    }
    /**
     * 使用jwt进行封装 返回
     * @param userInfoDTO
     * @return
     */
    public static String jwt(UserInfoDTO userInfoDTO){
        if(userInfoDTO!=null){
            userInfoDTO.setLoginTime(System.currentTimeMillis());
            String playload= JSON.toJSONString(userInfoDTO);
            String token = JWT.create().withAudience(playload).sign(Algorithm.HMAC256(JwtSecret.SECRET));
            return token;
        }
        return "";
    }
}
